package com.complex.spring.advance.service.resource;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class TestResourceLoader implements ResourceLoaderAware {

	public ResourceLoader resourceLoader;

	private static String arg1;

	private static String arg3;

	private static Date arg4;

	private static Resource template;
	
	public static void main(String[] args) {
		try {
			// ResourceLoader的默认构造DefaultResourceLoader
			// ResourceLoader resourceLoader = new DefaultResourceLoader();
			// resourceLoader.getResource("/xxxx.xml");

			/** ApplicationContext 作为ResourceLoader的角色 */
			ApplicationContext resourceLoader = new ClassPathXmlApplicationContext(
					"/bean/spring-service.xml");
			Resource resource = resourceLoader
					.getResource("http://www.baidu.com/");

			String host = resource.getURL().getHost();
			System.out.println(host);

			/** 注入resourceLoader,可以通过setter注入一个DefaultResourceLoader */
			/**
			 * 可以使用ResourceLoaderAware或者ApplicationContextAware
			 * 无论注入resourceLoader,或者注入applicationContext
			 */
			/** 这样虽然要依赖spring api,但同时也可以省去bean的定义 */
			resourceLoader.getResource("http://www.baidu.com/");
			host = resource.getURL().getHost();
			System.out.println(host);

			/** BeanFactoryPostProcessor的使用 */
			System.out.println(arg1);
			System.out.println(arg3);
			System.out.println(arg4);
			
			/**Resource注入*/
			System.out.println(template.getFilename());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Resource getTemplate() {
		return template;
	}

	public void setTemplate(Resource template) {
		this.template = template;
	}

	public Date getArg4() {
		return arg4;
	}

	public void setArg4(Date arg4) {
		TestResourceLoader.arg4 = arg4;
	}

	public String getArg3() {
		return arg3;
	}

	public void setArg3(String arg3) {
		TestResourceLoader.arg3 = arg3;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	public String getArg1() {
		return arg1;
	}

	public ResourceLoader getResourceLoader() {
		return resourceLoader;
	}

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

}
